package com.hopedove.quanlitycheck.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hopedove.quanlitycheck.R;
import com.hopedove.quanlitycheck.entity.MateriaEntity;

import java.util.List;

public class MateriaAdapter extends BaseAdapter {

	private Context mContext;
	private onBtnClickListener mOnBtnClickListener;
	private List<MateriaEntity> mList;
	private int currentPosition = -1;

	public MateriaAdapter(Context context, List<MateriaEntity> list) {
		super();
		mContext = context;

		// 这里集合用=号赋值，改变内部productlist 外部list一样会变
		mList = list;

	}

	@Override
	public int getCount() {

		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
		final MateriaEntity entity;
		entity = (MateriaEntity) getItem(position);
		View view;
		final ViewHolder viewholder;
		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);

			viewholder = new ViewHolder(view);
			view.setTag(viewholder);
		} else {
			view = convertView;
			viewholder = (ViewHolder) view.getTag();
		}

		if (entity.isSelect()) {
			viewholder.checkBtn.setChecked(true);
		} else {
			viewholder.checkBtn.setChecked(false);
		}


		/****0:未保存过   1： 保存过***/
		if (entity.getCHECKED().equals("1")) {
			view.setBackgroundColor(mContext.getResources().getColor(R.color.tran_black));
		} else {
			view.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
		}


		viewholder.info_tv.setText(entity.getSZQC());
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				for (MateriaEntity materiaEntity : mList) {
					materiaEntity.setSelect(false);
				}

				entity.setSelect(!entity.isSelect());

				notifyDataSetChanged();

			}
		});

		return view;

	}

	/**
	 * 设置接口
	 *
	 * @param
	 */
	public void setOnBtnClickListener(onBtnClickListener onBtnClickListener) {
		mOnBtnClickListener = onBtnClickListener;
	}

	/**
	 * 接口
	 *
	 * @author zhu_yu
	 */
	public interface onBtnClickListener {
		public void onClick(int position);
	}

	/**
	 * 对控件实例进行缓存
	 *
	 * @author zhu_yu
	 * @Description<功能详细描述>
	 * @Version [版本号, 2017-5-26]
	 */
	class ViewHolder {

		private TextView info_tv;
		private TextView num_tv;
		private CheckBox checkBtn;

		public ViewHolder(View view) {

			info_tv = (TextView) view.findViewById(R.id.info_tv);
			num_tv = (TextView) view.findViewById(R.id.num_tv);
			checkBtn = (CheckBox) view.findViewById(R.id.checkBox);
		}

	}

	private int mItemHeight;

	public void setItemHeight(int itemHeight) {
		// TODO Auto-generated method stub
		mItemHeight = itemHeight;
	}

}
